/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on Sep 23, 2010, 9:08:13 PM
 */

package cat200project;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Akmal
 */
public class CUS_RESERVATION extends javax.swing.JFrame {
    
    Statement stat;
    Connection con;
    ResultSet rset;
    private String url = "jdbc:oracle:thin:@localhost:1521:XE";
    private String pass_id = "restaurant";
    private String pass_pw = "restaurant";

    private final static String newline = "\n";
    
    /** Creates new form NewJFrame */
    public CUS_RESERVATION() throws SQLException {
        super("RESERVATION");
        initComponents();
        this.setLocationRelativeTo(null);
        connect();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        TableNum = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        PaymentDeposit = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        Save = new javax.swing.JButton();
        Back = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        PaymentType = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        AmountPerson = new javax.swing.JComboBox();
        DayReserve = new javax.swing.JComboBox();
        PhoneNum = new javax.swing.JTextField();
        FirstName = new javax.swing.JTextField();
        LastName = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        MonthReserve = new javax.swing.JComboBox();
        YearReserve = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        TableNum.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select...", "1", "2", "3", "4", "5", "6" }));
        TableNum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TableNumActionPerformed(evt);
            }
        });
        TableNum.setBounds(280, 170, 70, 30);
        jLayeredPane1.add(TableNum, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel5.setText("TABLE NUMBER:");
        jLabel5.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jLabel5.setBounds(40, 170, 100, 30);
        jLayeredPane1.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        PaymentDeposit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PaymentDepositActionPerformed(evt);
            }
        });
        PaymentDeposit.setBounds(270, 290, 170, 20);
        jLayeredPane1.add(PaymentDeposit, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel2.setText("FIRST NAME:");
        jLabel2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jLabel2.setBounds(40, 40, 110, 40);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel1.setText("MAKE RESERVATION");
        jLabel1.setBounds(30, 10, 340, 30);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Save.setFont(new java.awt.Font("Times New Roman", 1, 10)); // NOI18N
        Save.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        Save.setText("SAVE");
        Save.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        Save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveActionPerformed(evt);
            }
        });
        Save.setBounds(377, 350, 90, 30);
        jLayeredPane1.add(Save, javax.swing.JLayeredPane.DEFAULT_LAYER);

        Back.setFont(new java.awt.Font("Times New Roman", 1, 10));
        Back.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        Back.setText("BACK");
        Back.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        Back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackActionPerformed(evt);
            }
        });
        Back.setBounds(280, 350, 90, 30);
        jLayeredPane1.add(Back, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel10.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel10.setText("PHONE NUMBER:");
        jLabel10.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jLabel10.setBounds(40, 130, 100, 40);
        jLayeredPane1.add(jLabel10, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel7.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel7.setText("PAYMENT DEPOSIT:");
        jLabel7.setBounds(40, 280, 120, 40);
        jLayeredPane1.add(jLabel7, javax.swing.JLayeredPane.DEFAULT_LAYER);

        PaymentType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select...", "Cash", "Credit Card" }));
        PaymentType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PaymentTypeActionPerformed(evt);
            }
        });
        PaymentType.setBounds(230, 250, 80, 30);
        jLayeredPane1.add(PaymentType, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel6.setText("PAYMENT TYPE:");
        jLabel6.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jLabel6.setBounds(40, 250, 130, 30);
        jLayeredPane1.add(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel4.setText("AMOUNT OF PERSON: ");
        jLabel4.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jLabel4.setBounds(40, 200, 160, 50);
        jLayeredPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel3.setText("DATE FOR RESERVATION:");
        jLabel3.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jLabel3.setBounds(40, 100, 200, 40);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        AmountPerson.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select...", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));
        AmountPerson.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AmountPersonActionPerformed(evt);
            }
        });
        AmountPerson.setBounds(230, 210, 80, 30);
        jLayeredPane1.add(AmountPerson, javax.swing.JLayeredPane.DEFAULT_LAYER);

        DayReserve.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Day", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        DayReserve.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DayReserveActionPerformed(evt);
            }
        });
        DayReserve.setBounds(234, 110, 70, 20);
        jLayeredPane1.add(DayReserve, javax.swing.JLayeredPane.DEFAULT_LAYER);

        PhoneNum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                PhoneNumActionPerformed(evt);
            }
        });
        PhoneNum.setBounds(230, 140, 210, 20);
        jLayeredPane1.add(PhoneNum, javax.swing.JLayeredPane.DEFAULT_LAYER);

        FirstName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FirstNameActionPerformed(evt);
            }
        });
        FirstName.setBounds(230, 50, 250, 20);
        jLayeredPane1.add(FirstName, javax.swing.JLayeredPane.DEFAULT_LAYER);
        LastName.setBounds(230, 80, 250, 20);
        jLayeredPane1.add(LastName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel8.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel8.setText("RM");
        jLabel8.setBounds(230, 280, 30, 40);
        jLayeredPane1.add(jLabel8, javax.swing.JLayeredPane.DEFAULT_LAYER);

        MonthReserve.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }));
        MonthReserve.setBounds(317, 110, 80, 20);
        jLayeredPane1.add(MonthReserve, javax.swing.JLayeredPane.DEFAULT_LAYER);

        YearReserve.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Year", "2010", "2011", "2012", "2013", "2014", "2015" }));
        YearReserve.setBounds(410, 110, 60, 20);
        jLayeredPane1.add(YearReserve, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator1.setBounds(0, 40, 500, 30);
        jLayeredPane1.add(jSeparator1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jSeparator2.setBounds(0, 330, 500, 30);
        jLayeredPane1.add(jSeparator2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel11.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel11.setText("LAST NAME :");
        jLabel11.setBounds(40, 70, 140, 40);
        jLayeredPane1.add(jLabel11, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel12.setFont(new java.awt.Font("Times New Roman", 1, 12));
        jLabel12.setText("TABLE");
        jLabel12.setBounds(230, 174, 50, 20);
        jLayeredPane1.add(jLabel12, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/abstract_0072.jpg"))); // NOI18N
        jLabel9.setBounds(-80, -70, 1190, 590);
        jLayeredPane1.add(jLabel9, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void FirstNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FirstNameActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_FirstNameActionPerformed

    private void BackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackActionPerformed
        new MENU_LOGIN().setVisible(true);
        this.setVisible(false);

        // TODO add your handling code here:
}//GEN-LAST:event_BackActionPerformed

    private void PaymentDepositActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PaymentDepositActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_PaymentDepositActionPerformed

    private void AmountPersonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AmountPersonActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_AmountPersonActionPerformed

    private void TableNumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TableNumActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_TableNumActionPerformed

    private void PaymentTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PaymentTypeActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_PaymentTypeActionPerformed

    private void DayReserveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DayReserveActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_DayReserveActionPerformed

    private void PhoneNumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PhoneNumActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_PhoneNumActionPerformed

    private void SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveActionPerformed
        String a = FirstName.getText();
        String b = LastName.getText();
        String c = (String) DayReserve.getSelectedItem();
        String d = (String) MonthReserve.getSelectedItem();
        String e = (String) YearReserve.getSelectedItem();
        String date = c + "  " + d + "  "+ e ;
        String f = PhoneNum.getText();
        String g = (String) TableNum.getSelectedItem();
        String h = (String) AmountPerson.getSelectedItem();
        String i = (String) PaymentType.getSelectedItem();
        String j = PaymentDeposit.getText();

        String query = "INSERT INTO RESERVATION VALUES('"+ g +"','"+ a +"','"+ b +"','"+ h +"','"+ i +"',"+
                        "'"+ f +"','"+ j +"','"+ date +"')";
        if(a.equals("") || b.equals("") || f.equals("") || j.equals("") || c.equals("Day")
                || d.equals("Month") || e.equals("Year") || g.equals("Select...")
                || h.equals("Select...") || i.equals("Select..."))
        {
            JOptionPane.showMessageDialog(null, "Please fill in/select properly!");
        }
        else
        {
            String message = "Are you sure you want to make this reservation?";
            String title = "Confirmation";
            int reply = JOptionPane.showConfirmDialog(null, message , title , JOptionPane.YES_NO_OPTION);

            if(reply == JOptionPane.YES_OPTION)
            {
                try
                {
                    stat = con.createStatement();
                    stat.executeUpdate(query);

                    JOptionPane.showMessageDialog(null, "Reservation successful");
                    
                }
                catch (SQLException ex)
                {
                    Logger.getLogger(CUS_RESERVATION.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(null, "The choosen table is already been booked!");
                }
            }

            clearall();
        }

        // TODO add your handling code here:
    }//GEN-LAST:event_SaveActionPerformed

    private void clearall()
    {
        FirstName.setText("");
        LastName.setText("");
        DayReserve.setSelectedIndex(0);
        MonthReserve.setSelectedIndex(0);
        YearReserve.setSelectedIndex(0);
        PhoneNum.setText("");
        TableNum.setSelectedIndex(0);
        AmountPerson.setSelectedIndex(0);
        PaymentType.setSelectedIndex(0);
        PaymentDeposit.setText("");
    }

    private void connect() throws SQLException
    {
         try {
            //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
            System.out.println("Driver Loaded");
            con = DriverManager.getConnection(url,pass_id,pass_pw);
            System.out.println("Database Connected");

         }
         catch (Exception ex)
         {
            Logger.getLogger(MENU_SELECTION_MAIN.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new CUS_RESERVATION().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(CUS_RESERVATION.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox AmountPerson;
    private javax.swing.JButton Back;
    private javax.swing.JComboBox DayReserve;
    private javax.swing.JTextField FirstName;
    private javax.swing.JTextField LastName;
    private javax.swing.JComboBox MonthReserve;
    private javax.swing.JTextField PaymentDeposit;
    private javax.swing.JComboBox PaymentType;
    private javax.swing.JTextField PhoneNum;
    private javax.swing.JButton Save;
    private javax.swing.JComboBox TableNum;
    private javax.swing.JComboBox YearReserve;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    // End of variables declaration//GEN-END:variables

}
